package com.txby.wxpay;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.txby.pay.util.BeanToMapUtil;
/**
 * 微信支付交易处理
* @ClassName: WxTradeUtil 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author jacking
* @date 2017年10月30日 下午5:46:58 
 */
public class WxTradeUtilTest {
    private WXPay wxpay;
    private WXPayConfigImpl config;
    private String out_trade_no= "201613091059590000003489-asd002";
    private String total_fee="0.01";

    public WxTradeUtilTest() throws Exception {
//        config = WXPayConfigImpl.getInstance();wxd77ad63f47f83d46
//    	AppSecret(应用密钥)：c949c724b0353f78d46493aa6576f579
        config = WXPayConfigImpl.getInstanceNewObject("D:/txbyCert/apiclient_cert.p12", "wxd655b5106a5c43a2", "1387070702", "A0F9B00D126D486D28AA9A0F6FC8F88C");
        wxpay = new WXPay(config);
        total_fee = "0.01";
        // out_trade_no = "201701017496748980290321";
        out_trade_no = "201613091059590000003489-asd002";
    }
    public WxTradeUtilTest(boolean autoReport,boolean useSandbox) throws Exception {
//    	config = WXPayConfigImpl.getInstance();
    	  config = WXPayConfigImpl.getInstanceNewObject("d:/ddd.p12", "wxd655b5106a5c43a2", "1387070702", "6a5e21e9155afcdc890cf36524b64dcf");
//      沙盒
//      String key="64598364e622e14c68f999f946dd1241";
    	if(useSandbox) {
    		config.setKey("64598364e622e14c68f999f946dd1241");
    	}
    	
    	wxpay = new WXPay(config,autoReport, useSandbox);
    	total_fee = "0.01";
    	// out_trade_no = "201701017496748980290321";
    	out_trade_no = "201613091059590000003489-asd002";
    	
    }
    
    public WxTradeUtilTest(WXPayConfigImpl config) throws Exception {
    	config = config;
    	wxpay = new WXPay(config);
    }

    /**
     * 扫码支付  下单
     *  用户二维码扫描支付
     */
    public Map doUnifiedOrder( WXUnifiedOrderModel orderModel) {
    	Map result=null;
        HashMap<String, String> data = new HashMap<String, String>();
        orderModel=null!=orderModel?orderModel:new WXUnifiedOrderModel("腾讯充值中心-QQ会员充值", "201613091059590000003489-99", "", "CNY", "0.01", "127.0.0.1", "http://test.letiantian.me/wxpay/notify", "NATIVE", "12");
        // data.put("time_expire", "20170112104120");
        try { 
            Map<String, String> r = wxpay.unifiedOrder(BeanToMapUtil.convertBean(orderModel));
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    /**
     * 关闭订单
    * @Title: doOrderClose 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param     out_trade_no  商户订单号
    * @return void    返回类型 
    * @throws
     */
    public Map doOrderClose(String out_trade_no) {
    	Map result=null;
//        System.out.println("关闭订单");
        HashMap<String, String> data = new HashMap<String, String>();
        try {
            Map<String, String> r = wxpay.closeOrder(data);
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }
	/**
	 * 查询订单
	* @Title: doOrderQuery 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param out_trade_no     商户订单号
	* @return void    返回类型 
	* @throws
	 */
    public Map doOrderQuery(String out_trade_no) {
    	Map result=null;
//        System.out.println("查询订单");
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
//        data.put("transaction_id", "4008852001201608221962061594");
        try {
            Map<String, String> r = wxpay.orderQuery(data);
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }
    /**
     * 撤销订单
    * @Title: doOrderReverse 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param out_trade_no     商户订单号
    * @return void    返回类型 
    * @throws
     */
    public Map doOrderReverse(String out_trade_no) {
//        System.out.println("撤销");
    	Map result=null;
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", out_trade_no);
//        data.put("transaction_id", "4008852001201608221962061594");
        try {
            Map<String, String> r = wxpay.reverse(data);
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }



    /**
     * 长链接转短链接
     * 测试成功
     */
    public void doShortUrl() {
        String long_url = "weixin://wxpay/bizpayurl?pr=etxB4DY";
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("long_url", long_url);
        try {
            Map<String, String> r = wxpay.shortUrl(data);
            System.out.println(r);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 
     * 退款
     * 已测试
    * @Title: doRefund 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param refundModel    设定文件 
    * @return void    返回类型 
    * @throws
     */
    public Map doRefund( WXRefundModel refundModel) {
    	Map result=null;
    	refundModel=null!=refundModel?refundModel:new WXRefundModel(out_trade_no, out_trade_no, "CNY", "0.0.1", "0.0.1", config.getMchID());
        try {
            Map<String, String> r = wxpay.refund(BeanToMapUtil.convertBean(refundModel));
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;

    }

   
    /**
     * 
     * 查询退款
     * 已经测试
    * @Title: doRefundQuery 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param out_trade_no    商户订单号
    * @return void    返回类型 
    * @throws
     */
    public Map doRefundQuery(String out_trade_no) {
    	Map result=null;
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("out_refund_no", out_trade_no);
        try {
            Map<String, String> r = wxpay.refundQuery(data);
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

   
    /**
     * 
     * 对账单下载
     * 已测试
    * @Title: doDownloadBill 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param     设定文件 
    * @return void    返回类型 
    * @throws
     */
    public Map doDownloadBill(String bill_date,String bill_type) {
    	Map result=null;
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("bill_date", bill_date);
        data.put("bill_type", bill_type);
        try {
            Map<String, String> r = wxpay.downloadBill(data);
            result=r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    public void doGetSandboxSignKey() throws Exception {
        WXPayConfigImpl config = WXPayConfigImpl.getInstance();
        HashMap<String, String> data = new HashMap<String, String>();
        data.put("mch_id", config.getMchID());
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        String sign = WXPayUtil.generateSignature(data, config.getKey());
        data.put("sign", sign);
        WXPay wxPay = new WXPay(config);
        String result = wxPay.requestWithoutCert("/sandbox/pay/getsignkey", data, 10000, 10000);
        System.out.println(result);
    }

 
 

    public static void main(String[] args) throws Exception {
//      WxTradeUtil dodo = new WxTradeUtil();
//      WxTradeUtil dodo = new WxTradeUtil(false ,true);
      WxTradeUtil dodo = new WxTradeUtil();
      

      // dodo.doGetSandboxSignKey();
/*        String out_trade_no=System.currentTimeMillis()+"";
      System.out.println("订单号："+out_trade_no);
      WXUnifiedOrderModel orderModel=new WXUnifiedOrderModel("test", out_trade_no, "", "CNY", "10", "127.0.0.1", "http://test.letiantian.me/wxpay/notify", "NATIVE", "12");
//      System.out.println("支付二维码:"+dodo.doUnifiedOrder(orderModel).get("code_url"));
      System.out.println( dodo.doUnifiedOrder(orderModel));*/
  	
  	
  	
  	WXRefundModel refundModel=new WXRefundModel("1510653061030", "1510653061030", "CNY", "10", "10", "1387070702");
      System.out.println( dodo.doRefund(refundModel));
//      dodo. doGetSandboxSignKey();
      // dodo.doOrderQuery();
      // dodo.doDownloadBill();
      // dodo.doShortUrl();
      // dodo.test001();
      // dodo.doOrderQuery();
      // dodo.doOrderClose();
//       dodo.doRefund();
      // dodo.doRefundQuery();
      // dodo.doOrderReverse();
      // dodo.test001();
      // dodo.testUnifiedOrderSpeed();

/*       dodo.doOrderQuery();
      dodo.doOrderReverse();
      dodo.doOrderQuery();
      dodo.doOrderReverse();
      dodo.doOrderQuery();*/
      
  	Map wxm=new HashMap();
	 	wxm.put("certPath", "D:/txbyCert/wxRefundApiclient_cert.p12");
	 	wxm.put("app_id", "wxd655b5106a5c43a2");
	 	wxm.put("mch_id", "1387070702");
	 	wxm.put("key", "A0F9B00D126D486D28AA9A0F6FC8F88C");
	 	wxm.put("notify_url", "http://api.eeesys.com/shopOrder/wxpay/notify");
	 	System.out.println(JSON.toJSON(wxm));
	 	wxm.clear();
	 	
	 	 String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDCDsISX2QyY7Jq" + 
	 			"AJxnHFitvJFRUnBvIq4+O3MoGA9iPY06XkIn8dKwkXPW4NX5hJvTfGV3LZWQVs6s" + 
	 			"K2UcEN2GZjyW956QBGM8v3+EI9dNeJXZp+ufIlwX1KP6pVJhLURp0ZC3e8G2+rYZ" + 
	 			"voBL+zHjz5WPK4BQA0R/G4V879WA5vT6Y/Hy3uFbCFfW8pTzCKacwTgxOmALrvlW" + 
	 			"IuDEXk7HuuTTTmTzUK8PJtqiVsAwWgNoijrDMgzU4rwjQ0IPxw9PYwcWXbLXwdp/" + 
	 			"DcPXX7AUKHxyT4KVG8/9HnXmojzrbWrgtQuvBV6ZVsx9+/+8hku2p4L/jFxKhR9Y" + 
	 			"YA0ZrAcJAgMBAAECggEAKkTSqvPSPFOQ8hvt9VusSTOQp5kAWoTAVVfXrn0HJMvp" + 
	 			"bjRHzrGSxdj41W4lzXLACmHHLB1mvYja/Y96N5HI3tR4EB4hkJcUEpBXsy2nYP2K" + 
	 			"Dl3QKdpvGtW7XgYSmo4kn7QBBKZVPIkBjzv6cBJn/IojtoqwKT189v7EpcELzUaf" + 
	 			"VIq/cKMQHrji35w1OXCju+tr9xug3TbLizO/XCKGVcCxJWzTrJ/wKTY9XkHCIIHH" + 
	 			"cfS2C/129jssIwdEGiSKqvYnMG813bXaeyBUspM/2xYgBO0AdJHeZnazFrHibww2" + 
	 			"iZCJr+qP6XVbhNTL4iH797+up/f1qoplpWhM10BHAQKBgQD+bnom0VYtcLqdTxWZ" + 
	 			"tYcIJaRuSBqHv6TAVf38lvy69/jrsxhafYjPChGYU57+j6egrO4xKc/hFsb2IlIK" + 
	 			"JZyJbGgzT2nsWBmmyGRLMGN0hJa/XyK5t+fRxkka7Gd1gtHQnjG+h6O0vEkGHbxe" + 
	 			"ZR+cz6/OqtNY8n3H4JldaKh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W35piYCbLaQ+S2RKzaw0UX7hfdpJPO9RUsgahoBM3stZgyxayjGcismGC3ss+TVLrGHhkedKTIRKAbCkLm16RbeAHMqSOJqBwbEyUM0EyiD9pT/uBmfS0gTnGpOjLepDlH6Wp6ZwIDAQAB";
	 	
	 	wxm.put("app_id", "2017111309908295");
	 	wxm.put("merchant_private_key", merchant_private_key);
	 	wxm.put("alipay_public_key", alipay_public_key);
	 	wxm.put("gatewayUrl", "https://openapi.alipay.com/gateway.do");
	 	wxm.put("notify_url", "http://api.eeesys.com/shopOrder/alipay/notify");
	 	System.out.println(JSON.toJSON(wxm));
      
    }
}
